package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CategoryAttribute;
import com.ruoyi.business.service.ICategoryAttributeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 类目属性关联Controller
 * 
 * @author ruoyi
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/business/attribute")
public class CategoryAttributeController extends BaseController
{
    @Autowired
    private ICategoryAttributeService categoryAttributeService;


    @ApiOperation("添加属性关联")
    @PreAuthorize("@ss.hasPermi('business:category:edit')")
    @Log(title = "类目属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addCategoryAttribute(@Validated @RequestBody CategoryAttribute link) {
        link.setCreateBy(getUsername());
        return toAjax(categoryAttributeService.addAttributeToCategory(link));
    }

    @ApiOperation("移除属性关联")
    @PreAuthorize("@ss.hasPermi('business:category:edit')")
    @Log(title = "类目属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult removeCategoryAttribute(@PathVariable Long[] ids) {
        return toAjax(categoryAttributeService.removeLinks(ids));
    }






    /**
     * 查询类目属性关联列表
     */
    @PreAuthorize("@ss.hasPermi('business:attribute:list')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryAttribute categoryAttribute)
    {
        startPage();
        List<CategoryAttribute> list = categoryAttributeService.selectCategoryAttributeList(categoryAttribute);
        return getDataTable(list);
    }


    /**
     * 导出类目属性关联列表
     */
//    @PreAuthorize("@ss.hasPermi('business:attribute:export')")
//    @Log(title = "类目属性关联", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CategoryAttribute categoryAttribute)
//    {
//        List<CategoryAttribute> list = categoryAttributeService.selectCategoryAttributeList(categoryAttribute);
//        ExcelUtil<CategoryAttribute> util = new ExcelUtil<CategoryAttribute>(CategoryAttribute.class);
//        util.exportExcel(response, list, "类目属性关联数据");
//    }

    /**
     * 获取类目属性关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:attribute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(categoryAttributeService.selectCategoryAttributeById(id));
    }


    /**
     * 修改类目属性关联
     */
    @PreAuthorize("@ss.hasPermi('business:attribute:edit')")
    @Log(title = "类目属性关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryAttribute categoryAttribute)
    {
        return toAjax(categoryAttributeService.updateCategoryAttribute(categoryAttribute));
    }
}
